package TankFrst;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame   {
    public static final int GAME_HIGH=600;//设置高度
    public static final int GAME_WEIGHT=800;//设置宽度

    Tank myTank =new Tank(50,50,true,Dir.STOP,this);

//集合储存爆炸，子弹，敌人坦克
    List<Explode> explodes=new ArrayList<Explode>();
    List<Missile> missiles=new ArrayList<Missile>();
    List<Tank> tanks=new ArrayList<Tank>();

    Image offScreenImage=null;

    @Override
    public void paint(Graphics g) {
        g.drawString("missiles count:"+missiles.size(),10,50);
        g.drawString("explores count:"+explodes.size(),10,70);
        g.drawString("Tanks    count:"+tanks.size(),10,90);

        //遍历子弹
        for (int i = 0; i < missiles.size(); i++) {
            Missile m=missiles.get(i);
            m.hitTanks(tanks);//打击敌方坦克
            m.hitTank(myTank);//打击自己的坦克
            m.draw(g);//画出子弹
        }

        //遍历爆炸图片
        for (int i = 0; i < explodes.size(); i++) {
            Explode e=explodes.get(i);
            e.draw(g);
        }

        //遍历坦克
        for (int i = 0; i < tanks.size(); i++) {
            Tank t=tanks.get(i);
            t.collidesWithTanks(tanks);
            t.draw(g);//画出敌方坦克
        }
        myTank.draw(g);//画出自己的坦克
    }
//利用双缓冲解决画面卡顿问题
    @Override
    public void update(Graphics g) {
        if (offScreenImage==null){
            offScreenImage=this.createImage(GAME_WEIGHT,GAME_HIGH);
        }
        Graphics gOffScreen=offScreenImage.getGraphics();
        Color c=gOffScreen.getColor();
        gOffScreen.setColor(Color.green);
        gOffScreen.fillRect(0,0,GAME_WEIGHT,GAME_HIGH);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage,0,0,null);
    }

    public void lauchFrame() {
        this.setLocation(400, 300);//设置窗口的位置
        this.setSize(GAME_WEIGHT, GAME_HIGH);//设置窗口可见
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//设置窗口退出
            }
        });
        this.setResizable(false);//设置窗口不能变换大小
        this.setBackground(Color.green);
        this.addKeyListener(new KeyMonitor());//设置监听器
        setVisible(true);
        new Thread(new PaintThread()).start();//设置线程启动
    }

    public static void main(String[] args) {
        TankClient tankClient = new TankClient();
        tankClient.lauchFrame();
    }

//设置线程
    private class PaintThread  implements Runnable {
        @Override
        public void run() {
            while (true){
                repaint();//重绘
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //定义监听器
    private class KeyMonitor extends KeyAdapter{
        //键盘按下的监听器
        public void keyPressed(KeyEvent e) {
            myTank.keyPressed(e);
        }
        @Override
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);
        }
        //键盘松开的监听器
    }
    // STOPSHIP: 2022/4/20 12：00
}

